﻿using MyMusicPlay._4._0.Operate;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;

namespace MyMusicPlay._4._0
{
    /// <summary>
    /// 音乐播放进度条
    /// </summary>
    public class ScrollToPlaying : BaseClass
    {
        /// <summary>
        /// 点
        /// </summary>
        private StackPanel Dot { get { return mainWindow.Scroll_Play_Dot; } }

        /// <summary>
        /// 线
        /// </summary>
        private Canvas Line { get { return mainWindow.Scroll_Play_Line; } }

        private Thickness StateThickness
        {
            get
            {
                return new Thickness(-10, -4, 0, 0);
            }
        }

        private Thickness EndThickness
        {
            get
            {
                var left = Line.ActualWidth - (Dot.ActualWidth / 2);
                var top = (Dot.ActualHeight - Line.ActualHeight) / 2 * (-1);
                return new Thickness(left, top, 0, 0);
            }
        }

        /// <summary>
        /// 进度条动画
        /// </summary>
        public Storyboard Anim = new Storyboard();

        private MainWindow mainWindow;

        //暂停时记录的位置
        private Thickness TimeOutMargin = new Thickness();

        public ScrollToPlaying(MainWindow main)
        {
            mainWindow = main;
        }

        public void Initialize()
        { }

        /// <summary>
        /// 开始移动
        /// </summary>
        /// <param name="IsCurr">true:从当前位置开始播放  false:从touch开始播放</param>
        public void State(bool IsCurr)
        {
            double timeCount = PlayApplication.GetModule<Media>().GetMusicTimeCount();
            if (timeCount != 0)
            {
                if (IsCurr)
                    Animate.Move_IsStop(Anim, Dot, Dot.Margin, EndThickness, timeCount);
                else
                    Animate.Move_IsStop(Anim, Dot, StateThickness, EndThickness, timeCount);
                return;
            }
        }

        public void StateByTimeOut()
        {
            double timeCount = PlayApplication.GetModule<Media>().GetMusicTimeCount();
            Animate.Move_IsStop(Anim, Dot, TimeOutMargin, EndThickness, timeCount);
        }

        public void End()
        {
            Anim.Remove();
        }

        public void UpdateDotPoi(double X, double newX)
        {
            var length = Line.ActualWidth;
            var maegin = Dot.Margin;
            var x = (newX - X);
            if (maegin.Left <= -10 + length && maegin.Left >= -10)
            {
                Dot.Margin = new Thickness(maegin.Left + x, maegin.Top, maegin.Right, maegin.Bottom);
            }
        }

        /// <summary>
        /// 暂停动画
        /// </summary>
        public void TimeOutAnim()
        {
            Animate.FixedElement(Anim, Dot);
            TimeOutMargin = Dot.Margin;
        }

        /// <summary>
        /// 更新暂停位置
        /// </summary>
        public void UpdateTimeOutMargin()
        {
            TimeOutMargin = Dot.Margin;
        }

        /// <summary>
        /// 获取当前应该播放音乐的时间
        /// </summary>
        /// <returns></returns>
        public double GetCurrPlayTime()
        {
            var media = PlayApplication.GetModule<Media>();
            var left = Dot.Margin.Left + 10;
            var allWhite = Line.ActualWidth;
            return (left / allWhite) * media.GetMusicTimeAllCount();
        }
    }
}
